package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.GoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserAddressBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserInfoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.BuyOneGoodsModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetGoodsInfoModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetUserAddressByIdModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.UserInfoModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class BuyOneGoodsActivity extends BaseActivity implements View.OnClickListener {
    private String good_id, shop_name;
    private boolean up = false;

    private TextView buyone_shopname, buyone_goodprice, buyone_goodname, buyone_goodnum, buyone_goodsnum, buyone_xiaojiprice, buyone_allprice;
    TextView buyone_add,buyone_desc,buyone_tjdd;
    ImageView buyone_goodimg,buyone_fh;
    LinearLayout buyone_gotoaddresslist;
    EditText buyone_comment;
    TextView buyone_flagnum;
    double goodsprice;

    private int goodsnum;
    private int nowsum;
    private String userid;
    private String useraddressid;
    TextView buyone_username, buyone_phone, buyone_address;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_buy_one_order);
        good_id = getIntent().getStringExtra("goodid");
        shop_name = getIntent().getStringExtra("shopname");
        goodsnum = getIntent().getIntExtra("goodsnum", 1);
        buyone_flagnum = (TextView)findViewById(R.id.buyone_flagnum);

        buyone_username = (TextView) findViewById(R.id.buyone_username);
        buyone_phone = (TextView) findViewById(R.id.buyone_phone);
        buyone_address = (TextView) findViewById(R.id.buyone_address);

        buyone_shopname = (TextView) findViewById(R.id.buyone_shopname);
        buyone_goodimg = (ImageView) findViewById(R.id.buyone_goodimg);
        buyone_fh = (ImageView)findViewById(R.id.buyone_fh);
        buyone_tjdd = (TextView)findViewById(R.id.buyone_tjdd);
        buyone_comment = (EditText)findViewById(R.id.buyone_comment);

        buyone_goodprice = (TextView) findViewById(R.id.buyone_goodprice);
        buyone_goodname = (TextView) findViewById(R.id.buyone_goodname);
        buyone_goodnum = (TextView) findViewById(R.id.buyone_goodnum);
        buyone_goodsnum = (TextView) findViewById(R.id.buyone_goodsnum);
        buyone_xiaojiprice = (TextView) findViewById(R.id.buyone_xiaojiprice);
        buyone_allprice = (TextView) findViewById(R.id.buyone_allprice);
        buyone_gotoaddresslist = (LinearLayout) findViewById(R.id.buyone_gotoaddresslist);

        buyone_add = (TextView)findViewById(R.id.buyone_add);
        buyone_desc = (TextView)findViewById(R.id.buyone_desc);


        buyone_shopname.setText(shop_name);
        buyone_goodnum.setText(String.valueOf(goodsnum));
        buyone_goodsnum.setText("X" + String.valueOf(goodsnum));

        buyone_flagnum.setText("共1件");

        userid = getid();
        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.getUserInfo(userid, userInfoBeanTListener);


        GetGoodsInfoModel getGoodsInfoModel = new GetGoodsInfoModel();
        getGoodsInfoModel.getGoodsInfo(good_id, goodsBeanTListener);




    }

    @Override
    void initEvent() {
        buyone_gotoaddresslist.setOnClickListener(this);
        buyone_add.setOnClickListener(this);
        buyone_desc.setOnClickListener(this);
        buyone_tjdd.setOnClickListener(this);
        buyone_fh.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }

    TListener<UserInfoBean> userInfoBeanTListener = new TListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean userInfoBean) {
            useraddressid = userInfoBean.getUseraddress_id();
            GetUserAddressByIdModel getUserAddressByIdModel = new GetUserAddressByIdModel();

            getUserAddressByIdModel.getUserAddressById(useraddressid, userAddressBeanTListener1);
            //Toast.makeText(BuyOneGoodsActivity.this,useraddressid,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(BuyOneGoodsActivity.this,"ku",Toast.LENGTH_SHORT).show();
        }
    };

    TListener<UserAddressBean> userAddressBeanTListener1 = new TListener<UserAddressBean>() {
        @Override
        public void onResponse(UserAddressBean userAddressBean) {

            if (userAddressBean.getUseraddress_id() == null){
                AlertDialog dlg = new AlertDialog.Builder(BuyOneGoodsActivity.this)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("你的收货地址为空，请添加收货地址")//显示类容
                        .setNegativeButton("取消",null)
                        .setPositiveButton("前往添加地址", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

//                                userid = getid();
//                                String orderaddress = buyone_address.getText().toString().trim();
//                                String ordercomment = buyone_comment.getText().toString().trim();
//                                String goodid = good_id;
//                                String goodnum = String.valueOf(goodsnum);
//                                String addressid = useraddressid;
//                                BuyOneGoodsModel buyOneGoodsModel = new BuyOneGoodsModel();
//                                buyOneGoodsModel.buyOneGoods(userid,orderaddress,ordercomment,goodid,goodnum,addressid,successBeanTListener);
                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();
            }
            buyone_username.setText(userAddressBean.getUseraddress_username());
            buyone_address.setText(userAddressBean.getUseraddress_city()+userAddressBean.getUseraddress_address());
            buyone_phone.setText(userAddressBean.getUseraddress_phone());
            // useraddressid= userAddressBean.getUseraddress_id();

        }

        @Override
        public void onFail(String msg) {
             Toast.makeText(BuyOneGoodsActivity.this,"ccc",Toast.LENGTH_SHORT).show();
        }
    };


    TListener<GoodsBean> goodsBeanTListener = new TListener<GoodsBean>() {
        @Override
        public void onResponse(GoodsBean goodsBean) {
            String img = Service.BASE_URL + "flower_impression/public/uploads/" + goodsBean.getGood_photo();
            Picasso.with(BuyOneGoodsActivity.this).load(img).into(buyone_goodimg);
            buyone_goodname.setText(goodsBean.getGood_name());
            buyone_goodprice.setText("￥" + String.valueOf(goodsBean.getGood_price()));
            buyone_xiaojiprice.setText(String.valueOf(goodsnum * goodsBean.getGood_price()));
            buyone_allprice.setText("￥" + String.valueOf(goodsnum * goodsBean.getGood_price()));
            nowsum  = goodsBean.getGood_num();
            goodsprice = goodsBean.getGood_price();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buyone_gotoaddresslist:
                if (buyone_address.getText().toString().trim().equals("")){
                    Intent intent4 = new Intent(BuyOneGoodsActivity.this,AddAddressressActivity.class);
                    startActivity(intent4);
                    Toast.makeText(BuyOneGoodsActivity.this,"收货地址为空，请添加收货地址",Toast.LENGTH_SHORT).show();
                }
                else {
                    Intent intent = new Intent(BuyOneGoodsActivity.this, SelectUserAddressActivity.class);
                    intent.putExtra("useraddressid", useraddressid);
                    startActivity(intent);
                }

                break;

            case R.id.buyone_add:
                add();
                break;
            case R.id.buyone_desc:
                desc();
                break;
            case R.id.buyone_tjdd:


                AlertDialog dlg = new AlertDialog.Builder(BuyOneGoodsActivity.this)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("选择操作")//显示类容
                        .setNegativeButton("取消",null)
                        .setNeutralButton("稍后支付",null)
                        .setPositiveButton("支付宝", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                userid = getid();
                                String orderaddress = buyone_address.getText().toString().trim();
                                String ordercomment = buyone_comment.getText().toString().trim();
                                String goodid = good_id;
                                String goodnum = String.valueOf(goodsnum);
                                String addressid = useraddressid;
                                if (useraddressid == null || orderaddress == null){
                                    Intent intent1 = new Intent(BuyOneGoodsActivity.this,AddAddressressActivity.class);
                                    startActivity(intent1);
                                    Toast.makeText(BuyOneGoodsActivity.this,"收货地址为空，请添加收货地址",Toast.LENGTH_SHORT).show();
                                }
                                else{

                                  //  Toast.makeText(BuyOneGoodsActivity.this,"userid--"+userid+"orderaddress--"+orderaddress+"ordercomment--"+ordercomment+"goodid--"+goodid+"goodnum--"+goodnum+"addressid--"+addressid,Toast.LENGTH_LONG).show();
                                    BuyOneGoodsModel buyOneGoodsModel = new BuyOneGoodsModel();
                                    buyOneGoodsModel.buyOneGoods(userid,orderaddress,ordercomment,goodid,goodnum,addressid,successBeanTListener1);
                                }

                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();
                break;
            case R.id.buyone_fh:
                finish();
                break;
        }
    }

    TListener<SuccessBean> successBeanTListener1 = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {

            if (successBean.getSuccess().equals("1")){
                Toast.makeText(BuyOneGoodsActivity.this,"购买成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else{
                Toast.makeText(BuyOneGoodsActivity.this,successBean.getSuccess(),Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(BuyOneGoodsActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
            Toast.makeText(BuyOneGoodsActivity.this, msg, Toast.LENGTH_SHORT).show();
            Log.d("on fail", "onFail: "+msg);
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }

    private void add(){
        if (goodsnum < nowsum){
            goodsnum++;
            buyone_goodnum.setText(String.valueOf(goodsnum));
            buyone_xiaojiprice.setText(String.valueOf(goodsnum * goodsprice));
            buyone_allprice.setText("￥" + String.valueOf(goodsnum * goodsprice));
            buyone_goodsnum.setText("X" + String.valueOf(goodsnum));
        }
        else {
            Toast.makeText(BuyOneGoodsActivity.this,"超出库存",Toast.LENGTH_SHORT).show();
        }
    }
    private void desc(){
        if (goodsnum > 1){
            goodsnum--;
            buyone_goodnum.setText(String.valueOf(goodsnum));
            buyone_xiaojiprice.setText(String.valueOf(goodsnum * goodsprice));
            buyone_allprice.setText("￥" + String.valueOf(goodsnum * goodsprice));
            buyone_goodsnum.setText("X" + String.valueOf(goodsnum));
        }
        else {
            Toast.makeText(BuyOneGoodsActivity.this,"请最少选择一份",Toast.LENGTH_SHORT).show();
        }
    }
}